package com.wyt.web;

import groovy.util.logging.Slf4j;
import org.jsoup.Jsoup;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import static org.springframework.web.bind.annotation.RequestMethod.GET;


/**
 * Created by wyt on 2019/3/12.
 */
@Controller
@Slf4j
public class TestCtroller {

    private final String token = "token=QHJB,QHJB";

    private final String ArcgisHost2 = "http://7.170.62.10:83/arcgis/XCCG-Service/cgvectornew/MapServer";

    @RequestMapping(value = "/arcgis/XCCG-Service/cgvectornew/MapServer", method = GET)
    @ResponseBody
    public void mapServer1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String originUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + request.getRequestURI() + "?" + request.getQueryString();
        System.out.println(originUrl);
        String url = ArcgisHost2 + "?f=json&" + token;
        System.out.println("Arcgis:" + url);

        PrintWriter writer = response.getWriter();
        writer.write(post(url));
        response.setContentType("application/json;charset=UTF-8");
        writer.close();
    }

    @GetMapping("/arcgis/XCCG-Service/cgvectornew/MapServer/{params}")
    public void f1(@PathVariable String params, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String originUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + request.getRequestURI() + "?" + request.getQueryString();
        System.out.println(originUrl);
        String url = ArcgisHost2 + "/" + params + "?" + request.getQueryString() + "&" + token;
//        PrintWriter writer = response.getWriter();
//        writer.write(post(url));
////        response.setContentType("text/html;charset=UTF-8");
//        writer.close();

//        InputStream in = new ByteArrayInputStream(str.getBytes("UTF-8"));
        // 3：从response对象获取输出流
        InputStream in = null;
        OutputStream out = response.getOutputStream();
        try {

            HttpURLConnection conn = null;
            URL url1 = new URL(url);
            conn = (HttpURLConnection) url1.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            if (conn.getResponseCode() == 200) {
                in = conn.getInputStream();
            }

            // 4：将输入流中的数据读取出来，写到输出流中
            for (int b = -1; (b = in.read()) != -1; ) {
                out.write(b);
            }
            // 将图写到页面上，用输出流写
            out.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
            in.close();
        }
    }

    public String post(String url) throws IOException {
        Map<String, String> header = new HashMap<>(16);
        header.put("Accept", "*/*");
        header.put("Accept-Encoding", "gzip, deflate");
        header.put("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
        header.put("Content-Type", "application/json;charset=UTF-8");
        header.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0");
        return Jsoup.connect(url).headers(header).timeout(10000).ignoreContentType(true).execute().body();
    }
}
